package com.lanyou.cook.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

import com.lanyou.cook.entity.domain.InterfaceSchedule;



public class ExcelUtil {

	/**
	 * 导出用户的所有列表到excel
	 * @param userList 用户列表
	 * @param outputStream 输出流
	 */
	public static void exportUserExcel(List<InterfaceSchedule> interfaceSchedules, HSSFWorkbook workbook) {
		try {
		
			
			//1.1、创建合并单元格对象
			CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 4);//起始行号，结束行号，起始列号，结束列号
			
			//1.2、头标题样式
			HSSFCellStyle style1 = createCellStyle(workbook, (short)16);
			
			//1.3、列标题样式
			HSSFCellStyle style2 = createCellStyle(workbook, (short)13);
		    
			//2、创建工作表
			HSSFSheet sheet = workbook.createSheet("接口调度管理列表");
			//2.1、加载合并单元格对象
			sheet.addMergedRegion(cellRangeAddress);
			//设置默认列宽
			sheet.setDefaultColumnWidth(25);
			
			//3、创建行
			//3.1、创建头标题行；并且设置头标题
			HSSFRow row1 = sheet.createRow(0);
			HSSFCell cell1 = row1.createCell(0);
			//加载单元格样式
			cell1.setCellStyle(style1);
			cell1.setCellValue("接口调度管理列表");
			
			//3.2、创建列标题行；并且设置列标题
			HSSFRow row2 = sheet.createRow(1);
			String[] titles = {"领域","系统", "调度名", "接口", "周期","状态","调度开始时间","调度结束时间","上一次执行时间","下一次执行时间","错误通知人","备注"};
			for(int i = 0; i < titles.length; i++){
				HSSFCell cell2 = row2.createCell(i);
				//加载单元格样式
				cell2.setCellStyle(style2);
				cell2.setCellValue(titles[i]);
			}
			
			//4、操作单元格；将用户列表写入excel
			if(interfaceSchedules != null){
				for(int j = 0; j < interfaceSchedules.size(); j++){
					if(interfaceSchedules.get(j)!=null){
					HSSFRow row = sheet.createRow(j+2);
					HSSFCell cell11 = row.createCell(0);
					cell11.setCellValue(interfaceSchedules.get(j).getFieldName());
					HSSFCell cell12 = row.createCell(1);
					cell12.setCellValue(interfaceSchedules.get(j).getFieldSystemName());
					HSSFCell cell13 = row.createCell(2);
					cell13.setCellValue(interfaceSchedules.get(j).getName());
					HSSFCell cell14 = row.createCell(3);
					cell14.setCellValue(interfaceSchedules.get(j).getInterfaceName());
					HSSFCell cell15 = row.createCell(4);
					if("1".equals(interfaceSchedules.get(j).getRunWay())){
						cell15.setCellValue("运行一次");
					}else if("2".equals(interfaceSchedules.get(j).getRunWay())){
						cell15.setCellValue(interfaceSchedules.get(j).getRunCyc()+"秒");
					}else if("3".equals(interfaceSchedules.get(j).getRunWay())){
						cell15.setCellValue(interfaceSchedules.get(j).getRunCyc()+"分钟");
					}else if("4".equals(interfaceSchedules.get(j).getRunWay())){
						cell15.setCellValue(interfaceSchedules.get(j).getRunCyc()+"小时");
					}else if("5".equals(interfaceSchedules.get(j).getRunWay())){
						cell15.setCellValue(interfaceSchedules.get(j).getRunCyc()+"天");
					}else if("6".equals(interfaceSchedules.get(j).getRunWay())){
						cell15.setCellValue(interfaceSchedules.get(j).getRunCyc()+"周");
					}else if("7".equals(interfaceSchedules.get(j).getRunWay())){
						cell15.setCellValue(interfaceSchedules.get(j).getRunCyc()+"月");
					}
					
					HSSFCell cell16 = row.createCell(5);
					if("1".equals(interfaceSchedules.get(j).getRunStatus())){
						cell16.setCellValue("暂停");
					}else if("2".equals(interfaceSchedules.get(j).getRunStatus())){
						cell16.setCellValue("正常");
					}
					 DateFormat formater = new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss");
					HSSFCell cell17 = row.createCell(6);
					if(interfaceSchedules.get(j).getEndTime() !=null){
		             String beginTime = formater.format(interfaceSchedules.get(j).getBeginTime());
				
					cell17.setCellValue(beginTime);
					}else{
						cell17.setCellValue("");
					}
					HSSFCell cell18 = row.createCell(7);
					if(interfaceSchedules.get(j).getEndTime() !=null){
					String endTime = formater.format(interfaceSchedules.get(j).getEndTime());
					cell18.setCellValue(endTime);
					}else{
						cell18.setCellValue("");
					}
					HSSFCell cell19 = row.createCell(8);
					if(interfaceSchedules.get(j).getLastRunTime() !=null){
					String lastRunTime = formater.format(interfaceSchedules.get(j).getLastRunTime());
					cell19.setCellValue(lastRunTime);
					}else{
						cell19.setCellValue("");	
					}
					HSSFCell cell110 = row.createCell(9);
					if(interfaceSchedules.get(j).getNextRunTime()!=null){
						String nextRunTime = formater.format(interfaceSchedules.get(j).getNextRunTime());
						cell110.setCellValue(nextRunTime);
					}else{
						cell110.setCellValue("");
					}
					
					HSSFCell cell111 = row.createCell(10);
					cell111.setCellValue(interfaceSchedules.get(j).getErrSendTo());
					HSSFCell cell112 = row.createCell(11);
					cell112.setCellValue(interfaceSchedules.get(j).getRemark());
				}
				}
			}
			//5、输出
		    
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建单元格样式
	 * @param workbook 工作簿
	 * @param fontSize 字体大小
	 * @return 单元格样式
	 */
	private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontSize) {
		HSSFCellStyle style = workbook.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
		//创建字体
		HSSFFont font = workbook.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗字体
		font.setFontHeightInPoints(fontSize);
		//加载字体
		style.setFont(font);
		return style;
	}

}
